package org.jeecg.modules.zcgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.entity.DelFlagBaseEntity;
import org.jeecg.common.util.ReflectHelper;
import org.jeecg.modules.zcgl.entity.RepairFeedbackLog;
import org.jeecg.modules.zcgl.entity.RepairImage;
import org.jeecg.modules.zcgl.entity.RepairInfo;
import org.jeecg.modules.zcgl.enums.RepairHandleStatusEnum;
import org.jeecg.modules.zcgl.enums.RepairImageTypeEnum;
import org.jeecg.modules.zcgl.mapper.RepairFeedbackLogMapper;
import org.jeecg.modules.zcgl.mapper.RepairImageMapper;
import org.jeecg.modules.zcgl.mapper.RepairInfoMapper;
import org.jeecg.modules.zcgl.service.IRepairInfoService;
import org.jeecg.modules.zcgl.vo.RepairInfoVo;
import org.jeecg.modules.zcgl.vo.statistical.MaintenanceStatisticalVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 维修信息
 * @Author: jeecg-boot
 * @Date: 2020-06-19
 * @Version: V1.0
 */
@Service
public class RepairInfoServiceImpl extends ServiceImpl<RepairInfoMapper, RepairInfo> implements IRepairInfoService {

    @Autowired
    private RepairImageMapper repairImageMapper;
    @Autowired
    private RepairFeedbackLogMapper feedbackLogMapper;

    @Override
    @Transactional
    public boolean wxSaveOrUpdateByVo(RepairInfoVo vo) {
        this.checkModel(vo);
        RepairInfo repairInfo = new RepairInfo();
        BeanUtils.copyProperties(vo, repairInfo);
        //如果ID存在删除以前的图片信息
        if (StringUtils.isNotBlank(vo.getId())) {
            repairImageMapper.deletePhysicalByMainId(vo.getId(), RepairImageTypeEnum.CUSTOMER.getStatus());
        }
        this.saveOrUpdate(repairInfo);

        //保存图片信息
        saveImage(repairInfo, RepairImageTypeEnum.CUSTOMER.getStatus(), vo.getRepairImageList());
        return true;
    }

    @Override
    public RepairInfoVo getVoById(String id) {
        RepairInfo repairInfo = baseMapper.selectById(id);
        if (repairInfo != null) {
            RepairInfoVo vo = new RepairInfoVo();
            BeanUtils.copyProperties(repairInfo, vo);
            //查询用户报修登记的图片
            LambdaQueryWrapper<RepairImage> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RepairImage::getRepairId, repairInfo.getId());
            queryWrapper.eq(RepairImage::getType, RepairImageTypeEnum.CUSTOMER.getStatus());
            List<RepairImage> imageList = repairImageMapper.selectList(queryWrapper);
            vo.setRepairImageList(imageList);
            //查询维修后的反馈图片
            if (StringUtils.isNotBlank(repairInfo.getFeedbackId())) {
                LambdaQueryWrapper<RepairImage> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(RepairImage::getRepairId, repairInfo.getId());
                queryWrapper1.eq(RepairImage::getRepairFeedbackId, repairInfo.getFeedbackId());
                queryWrapper1.eq(RepairImage::getType, RepairImageTypeEnum.SYS_USER.getStatus());
                List<RepairImage> repairFeedbackImageList = repairImageMapper.selectList(queryWrapper1);
                vo.setRepairFeedbackImageList(repairFeedbackImageList);
            }
            return vo;
        }
        return null;
    }

    @Override
    @Transactional
    public boolean feedback(RepairInfoVo vo) {
        RepairInfo repairInfo = baseMapper.selectById(vo.getId());
        if (repairInfo == null) {
            throw new JeecgBootException("未查询到信息");
        }
//        BeanUtils.copyProperties(vo, repairInfo);
        repairInfo.setHandleStatus(RepairHandleStatusEnum.HANDLE_ENG.getStatus());
        repairInfo.setFeedbackDate(vo.getFeedbackDate() != null ? vo.getFeedbackDate() : new Date());
        repairInfo.setFeedbackContent(vo.getFeedbackContent());
        repairInfo.setHandleUserId(vo.getHandleUserId());
        repairInfo.setMoney(vo.getMoney());

        //保存反馈日志
        RepairFeedbackLog log = new RepairFeedbackLog();
        BeanUtils.copyProperties(repairInfo, log, ReflectHelper.getFiledName(DelFlagBaseEntity.class));
        feedbackLogMapper.insert(log);

        //设置最后的反馈日志ID信息
        repairInfo.setFeedbackId(log.getId());

        //保存图片信息
        saveImage(repairInfo, RepairImageTypeEnum.SYS_USER.getStatus(), vo.getRepairFeedbackImageList());

        baseMapper.updateById(repairInfo);
        return true;
    }

    @Override
    @Transactional
    public boolean saveByVo(RepairInfoVo vo) {
        this.checkModel(vo);
        RepairInfo repairInfo = new RepairInfo();
        BeanUtils.copyProperties(vo, repairInfo);
        baseMapper.insert(repairInfo);
        this.updateModel(repairInfo, vo.getRepairImageList(), vo.getRepairFeedbackImageList());
        return true;
    }

    @Override
    @Transactional
    public boolean updateByVo(RepairInfoVo vo) {
        this.checkModel(vo);
        RepairInfo repairInfo = new RepairInfo();
        BeanUtils.copyProperties(vo, repairInfo);

        //删除以前的图片信息
        repairImageMapper.deletePhysicalByMainId(vo.getId(), RepairImageTypeEnum.CUSTOMER.getStatus());

        this.updateModel(repairInfo, vo.getRepairImageList(), vo.getRepairFeedbackImageList());
        return true;
    }

    @Override
    public IPage<MaintenanceStatisticalVo> queryMaintenanceStatisticalPageList(Page<MaintenanceStatisticalVo> page, QueryWrapper<MaintenanceStatisticalVo> queryWrapper) {
        return baseMapper.queryMaintenanceStatisticalPageList(page,queryWrapper);
    }

    @Override
    public List<MaintenanceStatisticalVo> queryMaintenanceStatisticalList(QueryWrapper<MaintenanceStatisticalVo> queryWrapper) {
        return baseMapper.queryMaintenanceStatisticalPageList(queryWrapper);
    }

    private void updateModel(RepairInfo repairInfo, List<RepairImage> repairImageList, List<RepairImage> repairFeedbackImage){

        //重新保存反馈图片信息
        this.saveImage(repairInfo, RepairImageTypeEnum.CUSTOMER.getStatus(), repairImageList);

        if (repairInfo.getHandleStatus().intValue() == RepairHandleStatusEnum.PENDING.getStatus()){
            //待处理
            repairInfo.setHandleUserId("");
            repairInfo.setFeedbackId("");
        } else if (repairInfo.getHandleStatus().intValue() == RepairHandleStatusEnum.SYS_USER.getStatus()
                || repairInfo.getHandleStatus().intValue() == RepairHandleStatusEnum.HANDLE_ING.getStatus()) {
            //分配了处理人员
            if (StringUtils.isBlank(repairInfo.getHandleUserId())){
                throw new JeecgBootException("请选择维修人员！");
            }
            repairInfo.setHandleStatus(RepairHandleStatusEnum.HANDLE_ING.getStatus());
            repairInfo.setFeedbackId("");
        } else if (repairInfo.getHandleStatus().intValue() == RepairHandleStatusEnum.HANDLE_ENG.getStatus()){
            //处理完成
            if (StringUtils.isBlank(repairInfo.getHandleUserId())){
                throw new JeecgBootException("请选择维修人员！");
            }
            if (StringUtils.isBlank(repairInfo.getFeedbackContent())){
                throw new JeecgBootException("请输入维修反馈信息！");
            }
            if(repairInfo.getFeedbackDate() == null){
                repairInfo.setFeedbackDate(new Date());
            }

            //保存反馈日志
            RepairFeedbackLog log = new RepairFeedbackLog();
            BeanUtils.copyProperties(repairInfo, log, ReflectHelper.getFiledName(DelFlagBaseEntity.class));
            feedbackLogMapper.insert(log);

            //设置反馈ID
            repairInfo.setFeedbackId(log.getId());

            //保存反馈图片信息
            saveImage(repairInfo, RepairImageTypeEnum.SYS_USER.getStatus(), repairFeedbackImage);
        }
        baseMapper.updateById(repairInfo);
    }

    /**
     * 保存图片信息
     *
     * @param repairInfo
     * @param type
     * @param imageList
     */
    private void saveImage(RepairInfo repairInfo, int type, List<RepairImage> imageList) {
        if (imageList == null) {
            return;
        }
        imageList.stream().forEach(image -> {
            //如果是维修反馈，就设置反馈ID
            if (type == RepairImageTypeEnum.SYS_USER.getStatus()) {
                image.setRepairFeedbackId(repairInfo.getFeedbackId());
            }
            image.setRepairId(repairInfo.getId());
            image.setType(type);
            repairImageMapper.insert(image);
        });
    }

    /**
     * 检测实体验证
     *
     * @param vo
     */
    private void checkModel(RepairInfoVo vo) {
        if (StringUtils.isBlank(vo.getRepairTitle())) {
            throw  new JeecgBootException("请填写报修标题！");
        }
        if (StringUtils.isBlank(vo.getRepairContent())) {
            throw  new JeecgBootException("请填写报修内容！");
        }
        if (vo.getRepairDate() == null) {
            vo.setRepairDate(new Date());
        }
        if (vo.getHandleStatus() == null) {
            vo.setHandleStatus(RepairHandleStatusEnum.PENDING.getStatus());
        }
    }
}
